<?php

abstract class CriteriuForm extends BaseForm
{
    /** denumirea formularului (apare ca prefix al denumirilor campurilor) */
    const NAME = 'criteriu';

    // modulul in care se alfa template-urile formularelor
    const MODULE = 'criterii';
    const TEMPLATE_SUFFIX = 'form';

    protected $criteriu;

    public function __construct($criteriu, array $args = null)
    {
        $this->criteriu = $criteriu;

        parent::__construct(array(), array(), false);
    }

    public function getCriteriu()
    {
        return $this->criteriu;
    }

    public function getCriteriuCod()
    {
        return $this->criteriu->getCod();
    }

    public function getCriteriuId()
    {
        return $this->criteriu->getId();
    }

    public function __toString()
    {
        use_helper( 'Partial' );

        return get_partial( self::MODULE . '/' . self::TEMPLATE_SUFFIX, array( 'form' => $this ) );
    }

    public function getTemplate()
    {
        return self::MODULE . '/' . $this->getCriteriuCod().'_'.self::TEMPLATE_SUFFIX;
    }

    /**
     * Return the global post-validator for this form.
     * The validator class name has this template: [formClassName]PostValidator
     * E.g. form: CriteriuBeneficiarForm -> validator: CriteriuBeneficiarFormPostValidator
     *
     * @return sfValidatorBase A validator object
     */
    public function getPostValidator()
    {
        $validatorClass = get_called_class().'PostValidator';

        return class_exists($validatorClass) ?
            new $validatorClass :
            null;
    }

    public function save(array $values, $cheltuialaId)
    {
        foreach ($values as $field => $value)
        {
            // don't save NULL values
            if ($value === null) continue;

            $paramObject = new ParametruCheltuiala();
            $paramObject
                ->setCheltuialaId($cheltuialaId)
                ->setParametru($field)
                ->setValoare($value)
                ->save();
        }
    }

}
